package com.igeek.compus.backend.menu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeek.compus.backend.menu.dao.MenuDao;
import com.igeek.compus.backend.menu.entity.Menu;
import com.igeek.compus.backend.menu.service.MenuService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> getMenuListByPage(Menu menu) {
        if(menu.getPage() != null && menu.getLimit() != null) {
            PageHelper.startPage(menu.getPage(), menu.getLimit());
        }
        return menuDao.getMenuListByPage(menu);
    }

    @Override
    public Menu getMenuInfoById(Integer id) {
        return menuDao.getMenuInfoById(id);
    }

    @Override
    public int deleteMenuById(Menu menu) {
        return menuDao.deleteMenuById(menu);
    }

    @Override
    public int deleteMenuList(List<String> menuIdList) {
        return menuDao.deleteMenuList(menuIdList);
    }

    @Override
    public int addMenu(Menu menu) {
        return menuDao.addMenu(menu);
    }

    @Override
    public int updateMenu(Menu menu) {
        return menuDao.updateMenu(menu);
    }

    @Override
    public HSSFWorkbook menuExport(String[] titles, List<Menu> menuList) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();

//        CellStyle style = wb.createCellStyle();
////        style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
//        style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.GREEN.getIndex());
//        style.setBorderBottom(BorderStyle.THIN);
//        style.setBorderLeft(BorderStyle.THIN);
//        style.setBorderRight(BorderStyle.THIN);
//        style.setBorderTop(BorderStyle.THIN);

        HSSFRow head = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = head.createCell(i);
            cell.setCellValue(titles[i]);
        }

        for (int i = 0; i < menuList.size(); i++) {
            HSSFRow row = sheet.createRow(i + 2);
            row.setHeight((short) 350);
            Menu menu = menuList.get(i);

            HSSFCell cell1 = row.createCell(0);
//            cell1.setCellStyle(style);
            cell1.setCellValue(menu.getMenuName());

            HSSFCell cell2 = row.createCell(1);
            cell2.setCellValue(menu.getImageUrl());

            HSSFCell cell3 = row.createCell(2);
            cell3.setCellValue(menu.getUrl());

            HSSFCell cell4 = row.createCell(3);
            cell4.setCellValue(menu.getSerial());
        }

        return wb;
    }


}
